<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>📄 本地文件问答 Agent</title>
    <style>
        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            max-width: 800px;
            margin: 40px auto;
            padding: 20px;
            background-color: #f9f9f9;
        }
        .container {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        input, textarea, button {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 6px;
            box-sizing: border-box;
        }
        button {
            background: #4CAF50;
            color: white;
            font-weight: bold;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background: #45a049;
        }
        button:disabled {
            background: #cccccc;
            cursor: not-allowed;
        }
        #clearBtn {
            background: #f44336;
        }
        #clearBtn:hover {
            background: #d32f2f;
        }
        #answer {
            background: #f0f7ff;
            padding: 15px;
            border-radius: 6px;
            margin-top: 15px;
            white-space: pre-wrap;
            line-height: 1.5;
        }
        .status {
            color: #666;
            font-style: italic;
            margin-top: 5px;
            min-height: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>📄 本地文件问答 Agent</h1>
        
        <input type="file" id="fileInput" accept=".txt,.pdf">
        <div class="status" id="fileStatus"></div>
        
        <textarea id="question" placeholder="请输入你的问题..." rows="3"></textarea>
        
        <button onclick="ask()" id="askBtn">提问</button>
        <button onclick="clearAll()" id="clearBtn">清除</button>
        
        <div id="answer"></div>
        <div class="status" id="answerStatus"></div>
    </div>

    <script>
        let currentFile = null;

        document.getElementById('fileInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                currentFile = file;
                document.getElementById('fileStatus').innerText = `✅ 已选择文件: ${file.name}`;
            } else {
                currentFile = null;
                document.getElementById('fileStatus').innerText = '';
            }
        });

        async function ask() {
            const file = currentFile;
            const question = document.getElementById('question').value.trim();
            const askBtn = document.getElementById('askBtn');
            const answerDiv = document.getElementById('answer');
            const statusDiv = document.getElementById('answerStatus');

            if (!file) {
                alert("请先上传一个 .txt 或 .pdf 文件");
                return;
            }
            if (!question) {
                alert("请输入你的问题");
                return;
            }

            // 禁用按钮，显示状态
            askBtn.disabled = true;
            askBtn.innerText = "思考中...";
            answerDiv.innerText = "";
            statusDiv.innerText = "🚀 正在解析文件并调用大模型，请稍候...";

            try {
                const formData = new FormData();
                formData.append('file', file);
                formData.append('question', question);

                const response = await fetch('http://localhost:8000/ask', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) {
                    throw new Error(`请求失败: ${response.status}`);
                }

                const data = await response.json();
                answerDiv.innerText = data.answer || "未获取到答案";
                statusDiv.innerText = "✅ 回答完成";
            } catch (error) {
                console.error("请求出错:", error);
                answerDiv.innerText = "❌ 出错了！";
                statusDiv.innerText = `错误: ${error.message}`;
            } finally {
                askBtn.disabled = false;
                askBtn.innerText = "提问";
            }
        }

        function clearAll() {
            document.getElementById('fileInput').value = '';
            document.getElementById('question').value = '';
            document.getElementById('answer').innerText = '';
            document.getElementById('fileStatus').innerText = '';
            document.getElementById('answerStatus').innerText = '';
            currentFile = null;
        }
    </script>
</body>
</html>
